//仓库数据
var Ware = {
	//初始化
	init: function() {
		Ware.resaveCards([
			// {
			// 	"card": "E28068940000401893F2942A",
			// 	"img": "../static/pro/001.png",
			// 	"name": "古朴茶杯",
			// 	"status": 0, // 0 未入库 1入库 2出库
			// },
			{
				"card": "E28068940000401893F29429",
				"img": "../static/pro/002.png",
				"name": "MacBookAir笔记本",
				"status": 0, // 0 未入库 1入库 2出库
			},
			{
				"card": "E280689000000003180BD8A1",
				"img": "../static/pro/003.png",
				"name": "iphonese手机",
				"status": 0, // 0 未入库 1入库 2出库
			},
			{
				"card": "E280689000000003180C0E7D",
				"img": "../static/pro/004.png",
				"name": "小米3经典款",
				"status": 0, // 0 未入库 1入库 2出库
			},
			{
				"card": "E28068940000401893F2942Az",
				"img": "../static/pro/001.png",
				"name": "古朴茶杯1",
				"status": 0, // 0 未入库 1入库 2出库
			},
			{
				"card": "E28068940000401893F29429z",
				"img": "../static/pro/002.png",
				"name": "IphoneSe手机1",
				"status": 0, // 0 未入库 1入库 2出库
			},
			{
				"card": "E280689000000003180BD8A1z",
				"img": "../static/pro/003.png",
				"name": "小米3经典款1",
				"status": 0, // 0 未入库 1入库 2出库
			},
			{
				"card": "E280689000000003180C0E7Dz",
				"img": "../static/pro/004.png",
				"name": "MacBookAir笔记本1",
				"status": 0, // 0 未入库 1入库 2出库
			},
		]);
	},
	//获得仓库所有数据
	getCards: function() {
		return Ware.getCard();
	},
	//产出所有卡
	removeCard: function(card) {
		let cards = Ware.getCard();
		for (var i = 0; i < cards.length; i++) {
			try {
				if (cards[i].card == card) {
					cards.splice(i, 1);
					Ware.resaveCards(cards);
					break;
				}
			} catch (e) {
				console.log("删除err: ", e);
			}
		}
	},
	//入库
	InWareHome: function(card) {
		let cards = Ware.getCard();
		for (var i = 0; i < cards.length; i++) {
			try {
				if ((cards[i].card == card) && (cards[i].status != 1)) {
					cards[i].status = 1; //入库
					Ware.resaveCards(cards);
					return true;
				}
			} catch (e) {
				console.log("入库err: ", e);
			}
		}
		return false;
	},
	//根据卡号获得产品信息
	getProduct: function(card) {
		let cards = Ware.getCard();
		for (var i = 0; i < cards.length; i++) {
			try {
				if (cards[i].card == card) {
					 return cards[i];
				}
			} catch (e) {}
		};
		return null;
		
	},

	//出库
	OutWareHome: function(card) {
		let cards = Ware.getCard();
		for (var i = 0; i < cards.length; i++) {
			try {
				if ((cards[i].card == card) && (cards[i].status == 1)) {
					cards[i].status = 2; //出库
					Ware.resaveCards(cards);
					return true;
				}
			} catch (e) {
				console.log("出库err: ", e);
			}
		}
		return false;
	},


	//重置仓库
	resaveCards: function(cards) {
		localStorage.setItem("card", JSON.stringify(cards));
	},
	//添加新产品
	addCard: function(data) {
		let cards = Ware.getCard();
		let isexit = false;
		for (var i = 0; i < cards.length; i++) {
			try {
				if (cards[i].card == data.card) {
					isexit = true;
					break;
				}
			} catch (e) {}
		}
		if (!isexit) {
			cards.push(data);
			Ware.resaveCards(cards);
			return true;
		} else {

			return false;
		}
	},
	//获取库内信息
	getCard: function() {
		return JSON.parse(localStorage.getItem("card")) == null ? [] : JSON.parse(localStorage.getItem("card"));
	},
	//获得入库的个数
	getInWareSize: function() {
		let cards = Ware.getCard();
		let size = 0;
		for (var i = 0; i < cards.length; i++) {
			try {
				if (cards[i].status == 1) {
					size += 1;
				}
			} catch (e) {}
		}
		return size;
	},
	//获得出库的个数
	getOutWareSize: function() {
		let cards = Ware.getCard();
		let size = 0;
		for (var i = 0; i < cards.length; i++) {
			try {
				if (cards[i].status == 2) {
					size += 1;
				}
			} catch (e) {}
		}
		return size;
	}




}
